﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web.SystemConfig.Business;
using System.Data;
using ePlanet.Entities;
using Web.SystemConfig.Services;
using DreamBuilder.Core.Container;

namespace Web.Views.DataManage.PLuploads
{
    /// <summary>
    /// GetCodeTableHandler 的摘要说明
    /// </summary>
    public class GetCodeTableHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                context.Response.ContentType = "text/plain";
                CodeTable codeTable = new CodeTable();
                Web.SystemConfig.DataAccess.DALCodeTable iCodeTable = new Web.SystemConfig.DataAccess.DALCodeTable();
                DataSet KeepTimes = iCodeTable.GetCodeTableDataByTypeWithOutDBHelperGive("KeepTime");
                DataSet CarrierTypes = iCodeTable.GetCodeTableDataByTypeWithOutDBHelperGive("CarrierType");
                DataSet SecurityTypes = iCodeTable.GetCodeTableDataByTypeWithOutDBHelperGive("SecurityType");
                DataSet AccountTypes = iCodeTable.GetCodeTableDataByTypeWithOutDBHelperGive("AccountType");
                DataSet KeepSecurityTimes = iCodeTable.GetCodeTableDataByTypeWithOutDBHelperGive("KeepSecurityTime");
                DataSet Category = iCodeTable.GetSingleWellCategory();
                //新增列，列为字典表
                DataSet CatagoryType = iCodeTable.GetCodeTableDataByType("CatagoryType");

                if (KeepTimes != null && KeepTimes.Tables.Count > 0)
                {
                    foreach (DataRow row in KeepTimes.Tables[0].Rows)
                    {
                        Dict dict = new Dict();
                        dict.Key = row["CodeTableID"].ToString();
                        dict.Value = row["Code_Desc"].ToString();
                        codeTable.KeepTimes.Add(dict);
                    }
                }
                if (CarrierTypes != null && CarrierTypes.Tables.Count > 0)
                {
                    foreach (DataRow row in CarrierTypes.Tables[0].Rows)
                    {
                        Dict dict = new Dict();
                        dict.Key = row["CodeTableID"].ToString();
                        dict.Value = row["Code_Desc"].ToString();
                        codeTable.CarrierTypes.Add(dict);
                    }
                }
                if (SecurityTypes != null && SecurityTypes.Tables.Count > 0)
                {
                    foreach (DataRow row in SecurityTypes.Tables[0].Rows)
                    {
                        Dict dict = new Dict();
                        dict.Key = row["CodeTableID"].ToString();
                        dict.Value = row["Code_Desc"].ToString();
                        codeTable.SecretTypes.Add(dict);
                    }
                }
                if (AccountTypes != null && AccountTypes.Tables.Count > 0)
                {
                    foreach (DataRow row in AccountTypes.Tables[0].Rows)
                    {
                        Dict dict = new Dict();
                        dict.Key = row["CodeTableID"].ToString();
                        dict.Value = row["Code_Desc"].ToString();
                        codeTable.AccountTypes.Add(dict);
                    }
                }
                if (KeepSecurityTimes != null && KeepSecurityTimes.Tables.Count > 0)
                {
                    foreach (DataRow row in KeepSecurityTimes.Tables[0].Rows)
                    {
                        Dict dict = new Dict();
                        dict.Key = row["CodeTableID"].ToString();
                        dict.Value = row["Code_Desc"].ToString();
                        codeTable.KeepSecurityTimes.Add(dict);
                    }
                }
                if (Category != null && Category.Tables.Count > 0)
                {
                    //选择父节点
                    var category = Category.Tables[0].Select("CODE_TYPE='WellCategory'");

                    if (category != null && category.Length > 0)
                    {
                        foreach (DataRow dr in category)
                        {
                            Dict dict = new Dict();
                            dict.Key = dr["CodeTableID"].ToString();
                            dict.Value = dr["Code_Desc"].ToString();

                            var categoryDetail = Category.Tables[0].Select(string.Format("Code_Type='{0}'", dict.Key));

                            if (categoryDetail != null && categoryDetail.Length > 0)
                            {
                                dict.ChildDictList = new List<Dict>();
                                foreach (DataRow drDetail in categoryDetail)
                                {
                                    Dict dictDetail = new Dict();
                                    dictDetail.Key = drDetail["CodeTableID"].ToString();
                                    dictDetail.Value = drDetail["Code_Desc"].ToString();

                                    dict.ChildDictList.Add(dictDetail);
                                }
                            }

                            codeTable.Category.Add(dict);
                        }
                    }
                }
                if (CatagoryType != null && CatagoryType.Tables.Count > 0)
                {
                    foreach (DataRow row in CatagoryType.Tables[0].Rows)
                    {
                        Dict dict = new Dict();
                        dict.Key = row["CodeTableID"].ToString();
                        dict.Value = row["Code_Desc"].ToString();
                        codeTable.CatagoryType.Add(dict);
                    }
                }



                string codeTableJson = Newtonsoft.Json.JsonConvert.SerializeObject(codeTable);
                context.Response.Write(codeTableJson);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}